<?php
namespace app\equi\controller;

class Equi extends Base{


	public function __construct(){
		parent::__construct();
		$this->seal = model("Seal");
		$this->equi = model("Equipment");
		$this->option = model("Option");
	}

	// 设备绑定
	// http://192.168.200.99/equi/equi/initequi
	// UniqueId=qq378398666111111111211
	// &type=1001
	// &seal_num=4
	// &app_version=2.0
	// 
	public function bind(){
		$this->_mustPost();

		$post = input('');
		$rs = $this->equi->eqSaveData($post);

		if( isset($rs['code']) ){
			$this->res = array_merge($this->res,$rs);
			$this->res['data'] = nullToSpace($this->res['data']);
			die(myJSON($this->res));
		}else{
			$this->res['code']=999;
			$this->res['msg']=$rs;
			$this->res['data']='';
			die(myJSON($this->res));
		}

		die(myJSON($this->res));

		
	}

	/**
	 * 设备开机初始化
	 * @return [type] [description]
	 * http://192.168.200.99/equi/user/initequi
	 * UniqueId=qq378398666111111111211
	 * 
	 */
	public function initequi(){
		$this->_mustPost();

		$UniqueId = input('UniqueId');

		$rs  = $this->equi->where( 'equipment_code' , $UniqueId )->find();

		if( $rs ){
			$this->res['data']=$rs;
		}else{
			$this->res['code']=1101;
			$this->res['msg']='设备未绑定，请先绑定。';
			$this->res['data']='';
		}



		die(myJSON($this->res));
	}



	//修改设备信息 【名称 位置 简介】
	//http://192.168.200.99/equi/equi/uptinfo
	//UniqueId=qq37839866611111111121&id=23&equipment_name=1&equipment_address=2&equipment_info=3
	public function uptinfo(){
		$this->_mustPost();

		$post = input('');

		$equi = $this->equi->find($post['id']);

		if($equi['equipment_code'] != $post['UniqueId']){
			$this->res['code'] = 1111;
			$this->res['msg'] = 'id & UniqueId 信息有误！';
			die(myJSON($this->res));
		}

		$data= array();
		$data['id'] = input('id');
		$data['equipment_name'] = input('equipment_name');
		$data['equipment_address'] = input('equipment_address');
		$data['equipment_info'] = input('equipment_info');

		$rs = $this->equi->allowField(true)->isUpdate()->save($data);

		if($rs === false){
			$this->res['code'] = 9901;
			$this->res['msg'] = $this->equi->error() ;
		}


		die(myJSON($this->res));
	}









}